﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;
using System.Web.Security;

namespace BitPortal.Admin.Language
{
    public partial class Default : Controls.BaseAdminPage
    {
        protected override void OnInit(EventArgs e)
        {
            PermissionBLL.CheckRoleThrowException("LanguageView");
            base.OnInit(e);
            (this.LanguageAddEdit1 as Controls.BaseAdminControl).ReloadControls.Add(this.GridPanel1);
            btnDeletePermantly.Hidden = !Roles.IsUserInRole("RootAdmin");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            this.Store1.DataSource = BitPortal.Business.LanguageBLL.GetAll();
            this.Store1.DataBind();
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            sm.ClearSelections();
        }
        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            BindData();
        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            LanguageAddEdit1.BindData(null);
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                LanguageAddEdit1.BindData(sm.SelectedRows[0].RecordID);
            }
        }
        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                int record = 0;
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    if (BitPortal.Business.LanguageBLL.Delete(row.RecordID) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " ngôn ngữ thành công!");
                    BindData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }

        }
        protected void btnDeletePermantly_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("RootAdmin");
                int record = 0;
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    if (BitPortal.Business.LanguageBLL.DeletePermantly(row.RecordID) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " ngôn ngữ thành công!");
                    BindData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }

        }

    }
}